{extend name="../../admin/view/table"} {block name="button"}
<!--{if auth("add")}-->
<button class="layui-btn layui-btn-sm layui-btn-primary" data-open='{:url("add")}' data-title="添加商品">
  添加商品
</button>
<button
  data-table-id="FormTable"
  data-action='{:url("remove")}'
  data-rule="id#{id}"
  data-confirm="确定要删除选中？"
  class="layui-btn layui-btn-sm layui-btn-primary">
  {:lang('批量删除')}
</button>
<!--{/if}-->

{/block} {block name="content"}

<div class="think-box-shadow">
  {include file='product/shop/index_search'}
  <table id="FormTable" data-url="{:sysuri()}" data-target-search="form.form-search"></table>
</div>
{/block} {block name='script'}

<script>
  $(function () {
    $('#FormTable').layTable({
      even: true,
      height: 'full',
      height: 'full',
      limit: 20,

      cols: [
        [
          { checkbox: true, field: 'id' },
          { field: 'sort', title: '排序权重', width: 100, align: 'center', sort: true, templet: '#SortInputTpl' },
          { field: 'name', title: '商品中文名称', minWidth: 160 },
          { field: 'no', title: '商品编号', minWidth: 100 },
          { field: 'cat', title: '类别', minWidth: 160 },
          { field: 'update_time', title: '更新时间', minWidth: 170, align: 'center' },
          { field: 'status', title: '状态', minWidth: 120, align: 'center', templet: '#StatusSwitchTpl' },
          { toolbar: '#toolbar', title: '操作面板', minWidth: 150, align: 'center', fixed: 'right' }
        ]
      ]
    })

    // 数据状态切换操作
    layui.form.on('switch(StatusSwitch)', function (object) {
      console.log(object)
      object.data = { status: object.elem.checked == 0 ? 1 : 0 }
      object.data.id = object.value.split('|')[object.data.status] || object.value
      console.log(object.data)
      $.form.load(
        "{:url('state')}",
        object.data,
        'post',
        function (ret) {
          if (ret.code < 1)
            $.msg.error(ret.info, 3, function () {
              $('#FormTable').trigger('reload')
            })
          else {
            $('#FormTable').trigger('reload')
          }
          return false
        },
        false
      )
    })
  })
</script>

<!-- 数据状态切换模板 -->
<script type="text/html" id="StatusSwitchTpl">
  <!--{if auth("state")}-->
  <input type="checkbox" value="{{d.id}}|{{d.spp}}" lay-text="已上架|已下架" lay-filter="StatusSwitch" lay-skin="switch"
  {{-d.status==0?'checked':''}}>
  <!--{else}-->
  {{-d.status ? '
  <b class="color-green">已上架</b>
  ' : '
  <b class="color-red">已下架</b>
  '}}
  <!--{/if}-->
</script>

<!-- 列表排序权重模板 -->
<script type="text/html" id="SortInputTpl">
  <input
    type="number"
    min="0"
    data-blur-number="0"
    data-action-blur="{:sysuri()}"
    data-value="id#{{d.id}};action#sort;sort#{value}"
    data-loading="false"
    value="{{d.sort}}"
    class="layui-input text-center" />
</script>

<!-- 操控面板的模板 -->
<script type="text/html" id="toolbar">
  <!--{if auth('edit')}-->
  <a class="layui-btn layui-btn-sm" data-title="编辑商品分类" data-open='{:url("edit")}?id={{d.id}}'>编 辑</a>
  <!--{/if}-->
</script>
{/block}
